package com.shanwen.shopping.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.PointF;
import android.view.View;
import android.widget.ImageView;

import java.util.List;


/**
 *
 * Created by lex on 2017/4/20.
 */

public class MenuAnimationUtil {

    private List<ImageView> ivs;
    private final int radius = 250;
    private final int duration = 200;

    public MenuAnimationUtil(List<ImageView> ivs) {
        this.ivs = ivs;
    }

    public void showMenu() {
        AnimatorSet animatorSet = new AnimatorSet();
        for (int i = -2; i < ivs.size()-2; i++) {
            PointF point = new PointF();
            int avgAngle = (120 / (ivs.size() - 1));
            int angle = avgAngle * i;
            point.x = (float) Math.cos(angle * (Math.PI / 180)) * radius;
            point.y = (float) Math.sin(angle * (Math.PI / 180)) * radius;
            ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(ivs.get(i+2), "translationX", 0, point.x);
            ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(ivs.get(i+2), "translationY", 0, point.y);
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    for (int j = 0; j < ivs.size(); j++) {
                        ivs.get(j).setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onAnimationEnd(Animator animation) {

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animatorSet.setDuration(duration);
            animatorSet.play(objectAnimatorX).with(objectAnimatorY);
            animatorSet.start();
        }
    }

    public void closeMenu() {
        AnimatorSet animatorSet = new AnimatorSet();
        for (int i = -2; i < ivs.size()-2; i++) {
            PointF point = new PointF();
            int avgAngle = (120 / (ivs.size() - 1));
            int angle = avgAngle * i;
            point.x = (float) Math.cos(angle * (Math.PI / 180)) * radius;
            point.y = (float) Math.sin(angle * (Math.PI / 180)) * radius;
            ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(ivs.get(i+2), "translationX", point.x, 0);
            ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(ivs.get(i+2), "translationY", point.y, 0);
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    for (int j = 0; j < ivs.size(); j++) {
                        ivs.get(j).setVisibility(View.GONE);
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animatorSet.setDuration(duration);
            animatorSet.play(objectAnimatorX).with(objectAnimatorY);
            animatorSet.start();
        }
    }
}
